package cn.edu.controller;

import cn.edu.DAO.UserDAOByXML;
import cn.edu.DO.PlaceDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.naming.Name;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/trip")
public class PlaceController {
    @Autowired
    private UserDAOByXML userDAO;

    @RequestMapping("/totrip")
    public String totrip(){
        return "user/place";
    }

    @RequestMapping("/toaddTrip")
    public String toaddtrip(){
        return "gao/add";
    }

    @RequestMapping("/todeleteTrip")
    public String todeletetrip(){
        return "gao/delete";
    }
    @RequestMapping("/toupdataTrip")
    public String toupdatatrip(){
        return "gao/updata";
    }

    @RequestMapping("/addTrip")
    public String addtrip(HttpServletRequest Request, Map map){
        String name =Request.getParameter("name");
        String money =Request.getParameter("money");
        if (userDAO.insertplace(name,money)!=null){
            map.put("insertright",("插入成功"));
            return "index";
        }
        else{
            map.put("inserterror",("插入失败"));
            return("index");
        }
    }

    @RequestMapping("/deleteTrip")
    public String deletetrip(HttpServletRequest Request, Map map){
        String name =Request.getParameter("name");
        if (userDAO.deleteplaceByname(name)!=null){
            map.put("deleteright",("删除成功"));
            return "index";
        }
        else{
            map.put("deleteerror",("删除失败或无该旅游项目"));
            return("index");
        }
    }


    @RequestMapping("/getTriplist")
    public String getUserlist( Map map){
        List<PlaceDO> placelist = userDAO.selectAllplace();
        map.put("placelist",placelist);
        return "user/placelist";
    }

    @RequestMapping("/updataTrip")
    public String updatatrip(HttpServletRequest Request,Map map){
        String name =Request.getParameter("name");
        String newname =Request.getParameter("newname");
        String newmoney =Request.getParameter("newmoney");
        if (userDAO.updataplace(newname,newmoney,name)!=null){
            map.put("deleteright",("更新成功"));
            return "index";
        }
        else{
            map.put("deleteerror",("无该旅游项目"));
            return("index");
        }
    }
    }
